using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace ConfigFileTest
{
    [Serializable]
    public class Config
    {
        #region Private properties
        private const string DEFAULT_FILENAME = "defaultConfig.xml";
        private string _FileName = DEFAULT_FILENAME;
        private string _ApplicationName = "";
        private bool _StartApplicationMaximized = false;
        private int _MaxNumberDebugLines = 8;
        private TestObject _SerializedObject = null;
        #endregion

        #region Constructor(s)
        public Config()
        { }
        #endregion

        #region Public properties
        /// <summary>
        /// Gets or sets the name of the file.
        /// </summary>
        /// <value>The name of the file.</value>
        [XmlIgnore]
        public string FileName
        {
            get { return _FileName; }
            set { _FileName = value; }
        }

        /// <summary>
        /// Gets or sets the name of the application.
        /// </summary>
        /// <value>The name of the application.</value>
        public string ApplicationName
        {
            get { return _ApplicationName; }
            set { _ApplicationName = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [start application maximized].
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [start application maximized]; otherwise, <c>false</c>.
        /// </value>
        public bool StartApplicationMaximized
        {
            get { return _StartApplicationMaximized; }
            set { _StartApplicationMaximized = value; }
        }

        /// <summary>
        /// Gets or sets the max number debug lines.
        /// </summary>
        /// <value>The max number debug lines.</value>
        public int MaxNumberDebugLines
        {
            get { return _MaxNumberDebugLines; }
            set { _MaxNumberDebugLines = value; }
        }

        /// <summary>
        /// Gets or sets the serialized object.
        /// </summary>
        /// <value>The serialized object.</value>
        public TestObject SerializedObject
        {
            get { return _SerializedObject; }
            set { _SerializedObject = value; }
        }
        #endregion

        #region Public methods
        /// <summary>
        /// Loads the config.
        /// </summary>
        /// <returns></returns>
        public static Config Load()
        {
            return Load(DEFAULT_FILENAME);
        }

        /// <summary>
        /// Loads the config.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <returns></returns>
        public static Config Load(string filename)
        {
            try
            {
                FileStream fs = new FileStream(filename, FileMode.Open);
                XmlReader reader = new XmlTextReader(fs);

                XmlSerializer ser = new XmlSerializer(typeof(Config));
                Config config = (Config)ser.Deserialize(reader);
                if (config != null)
                    config.FileName = filename;

                return config;
            }
            catch (Exception err)
            {
                //throw new Exception(err.Message);
                return null;
            }
        }

        /// <summary>
        /// Saves the config.
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Config));
                Stream fs = new FileStream(this.FileName, FileMode.Create);
                XmlWriter writer = new XmlTextWriter(fs, new UTF8Encoding());
                serializer.Serialize(writer, this);
                writer.Close();
                
                return true;
            }
            catch (Exception err)
            {
                //throw new Exception(err.Message);
                return false;
            }
        }

        #endregion
    }

    [Serializable]
    public class TestObject
    {
        #region Private properties
        private string _TestString = "";
        private bool _TestBool = false;
        private int _TestInt = 8;
        #endregion

        #region Constructor(s)
        public TestObject()
        { }
        #endregion

        #region Public properties
        /// <summary>
        /// Gets or sets the test string.
        /// </summary>
        /// <value>The test string.</value>
        public string TestString
        {
            get { return _TestString; }
            set { _TestString = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [test bool].
        /// </summary>
        /// <value><c>true</c> if [test bool]; otherwise, <c>false</c>.</value>
        public bool TestBool
        {
            get { return _TestBool; }
            set { _TestBool = value; }
        }

        /// <summary>
        /// Gets or sets the test int.
        /// </summary>
        /// <value>The test int.</value>
        public int TestInt
        {
            get { return _TestInt; }
            set { _TestInt = value; }
        }
        #endregion
    }
}
